home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / python2.4 / pdb.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  33.0 KB  |  1,137 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''A Python debugger.'''
  5. import sys
  6. import linecache
  7. import cmd
  8. import bdb
  9. from repr import Repr
  10. import os
  11. import re
  12. import pprint
  13. import traceback
  14. _repr = Repr()
  15. _repr.maxstring = 200
  16. _saferepr = _repr.repr
  17. __all__ = [
  18.     'run',
  19.     'pm',
  20.     'Pdb',
  21.     'runeval',
  22.     'runctx',
  23.     'runcall',
  24.     'set_trace',
  25.     'post_mortem',
  26.     'help']
  27.  
  28. def find_function(funcname, filename):
  29.     cre = re.compile('def\\s+%s\\s*[(]' % funcname)
  30.     
  31.     try:
  32.         fp = open(filename)
  33.     except IOError:
  34.         return None
  35.  
  36.     lineno = 1
  37.     answer = None
  38.     while None:
  39.         line = fp.readline()
  40.         if line == '':
  41.             break
  42.         
  43.         if cre.match(line):
  44.             answer = (funcname, filename, lineno)
  45.             break
  46.         
  47.         lineno = lineno + 1
  48.     fp.close()
  49.     return answer
  50.  
  51. line_prefix = '\n-> '
  52.  
  53. class Pdb(bdb.Bdb, cmd.Cmd):
  54.     
  55.     def __init__(self):
  56.         bdb.Bdb.__init__(self)
  57.         cmd.Cmd.__init__(self)
  58.         self.prompt = '(Pdb) '
  59.         self.aliases = { }
  60.         self.mainpyfile = ''
  61.         self._wait_for_mainpyfile = 0
  62.         
  63.         try:
  64.             import readline
  65.         except ImportError:
  66.             pass
  67.  
  68.         self.rcLines = []
  69.         if 'HOME' in os.environ:
  70.             envHome = os.environ['HOME']
  71.             
  72.             try:
  73.                 rcFile = open(os.path.join(envHome, '.pdbrc'))
  74.             except IOError:
  75.                 pass
  76.  
  77.             for line in rcFile.readlines():
  78.                 self.rcLines.append(line)
  79.             
  80.             rcFile.close()
  81.         
  82.         
  83.         try:
  84.             rcFile = open('.pdbrc')
  85.         except IOError:
  86.             pass
  87.  
  88.         for line in rcFile.readlines():
  89.             self.rcLines.append(line)
  90.         
  91.         rcFile.close()
  92.  
  93.     
  94.     def reset(self):
  95.         bdb.Bdb.reset(self)
  96.         self.forget()
  97.  
  98.     
  99.     def forget(self):
  100.         self.lineno = None
  101.         self.stack = []
  102.         self.curindex = 0
  103.         self.curframe = None
  104.  
  105.     
  106.     def setup(self, f, t):
  107.         self.forget()
  108.         (self.stack, self.curindex) = self.get_stack(f, t)
  109.         self.curframe = self.stack[self.curindex][0]
  110.         self.execRcLines()
  111.  
  112.     
  113.     def execRcLines(self):
  114.         if self.rcLines:
  115.             rcLines = self.rcLines
  116.             self.rcLines = []
  117.             for line in rcLines:
  118.                 line = line[:-1]
  119.                 if len(line) > 0 and line[0] != '#':
  120.                     self.onecmd(line)
  121.                     continue
  122.             
  123.         
  124.  
  125.     
  126.     def user_call(self, frame, argument_list):
  127.         '''This method is called when there is the remote possibility
  128.         that we ever need to stop in this function.'''
  129.         if self._wait_for_mainpyfile:
  130.             return None
  131.         
  132.         if self.stop_here(frame):
  133.             print '--Call--'
  134.             self.interaction(frame, None)
  135.         
  136.  
  137.     
  138.     def user_line(self, frame):
  139.         '''This function is called when we stop or break at this line.'''
  140.         if self._wait_for_mainpyfile:
  141.             if self.mainpyfile != self.canonic(frame.f_code.co_filename) or frame.f_lineno <= 0:
  142.                 return None
  143.             
  144.             self._wait_for_mainpyfile = 0
  145.         
  146.         self.interaction(frame, None)
  147.  
  148.     
  149.     def user_return(self, frame, return_value):
  150.         '''This function is called when a return trap is set here.'''
  151.         frame.f_locals['__return__'] = return_value
  152.         print '--Return--'
  153.         self.interaction(frame, None)
  154.  
  155.     
  156.     def user_exception(self, frame, .4):
  157.         '''This function is called if an exception occurs,
  158.         but only if we are to stop at or just below this level.'''
  159.         (exc_type, exc_value, exc_traceback) = .4
  160.         frame.f_locals['__exception__'] = (exc_type, exc_value)
  161.         if type(exc_type) == type(''):
  162.             exc_type_name = exc_type
  163.         else:
  164.             exc_type_name = exc_type.__name__
  165.         print exc_type_name + ':', _saferepr(exc_value)
  166.         self.interaction(frame, exc_traceback)
  167.  
  168.     
  169.     def interaction(self, frame, traceback):
  170.         self.setup(frame, traceback)
  171.         self.print_stack_entry(self.stack[self.curindex])
  172.         self.cmdloop()
  173.         self.forget()
  174.  
  175.     
  176.     def default(self, line):
  177.         if line[:1] == '!':
  178.             line = line[1:]
  179.         
  180.         locals = self.curframe.f_locals
  181.         globals = self.curframe.f_globals
  182.         
  183.         try:
  184.             code = compile(line + '\n', '<stdin>', 'single')
  185.             exec code in globals, locals
  186.         except:
  187.             (t, v) = sys.exc_info()[:2]
  188.             if type(t) == type(''):
  189.                 exc_type_name = t
  190.             else:
  191.                 exc_type_name = t.__name__
  192.             print '***', exc_type_name + ':', v
  193.  
  194.  
  195.     
  196.     def precmd(self, line):
  197.         """Handle alias expansion and ';;' separator."""
  198.         if not line.strip():
  199.             return line
  200.         
  201.         args = line.split()
  202.         while args[0] in self.aliases:
  203.             line = self.aliases[args[0]]
  204.             ii = 1
  205.             for tmpArg in args[1:]:
  206.                 line = line.replace('%' + str(ii), tmpArg)
  207.                 ii = ii + 1
  208.             
  209.             line = line.replace('%*', ' '.join(args[1:]))
  210.             args = line.split()
  211.         if args[0] != 'alias':
  212.             marker = line.find(';;')
  213.             if marker >= 0:
  214.                 next = line[marker + 2:].lstrip()
  215.                 self.cmdqueue.append(next)
  216.                 line = line[:marker].rstrip()
  217.             
  218.         
  219.         return line
  220.  
  221.     do_h = cmd.Cmd.do_help
  222.     
  223.     def do_break(self, arg, temporary = 0):
  224.         if not arg:
  225.             if self.breaks:
  226.                 print 'Num Type         Disp Enb   Where'
  227.                 for bp in bdb.Breakpoint.bpbynumber:
  228.                     if bp:
  229.                         bp.bpprint()
  230.                         continue
  231.                 
  232.             
  233.             return None
  234.         
  235.         filename = None
  236.         lineno = None
  237.         cond = None
  238.         comma = arg.find(',')
  239.         if comma > 0:
  240.             cond = arg[comma + 1:].lstrip()
  241.             arg = arg[:comma].rstrip()
  242.         
  243.         colon = arg.rfind(':')
  244.         funcname = None
  245.         if colon >= 0:
  246.             filename = arg[:colon].rstrip()
  247.             f = self.lookupmodule(filename)
  248.             if not f:
  249.                 print '*** ', repr(filename), 'not found from sys.path'
  250.                 return None
  251.             else:
  252.                 filename = f
  253.             arg = arg[colon + 1:].lstrip()
  254.             
  255.             try:
  256.                 lineno = int(arg)
  257.             except ValueError:
  258.                 msg = None
  259.                 print '*** Bad lineno:', arg
  260.                 return None
  261.             except:
  262.                 None<EXCEPTION MATCH>ValueError
  263.             
  264.  
  265.         None<EXCEPTION MATCH>ValueError
  266.         
  267.         try:
  268.             lineno = int(arg)
  269.         except ValueError:
  270.             
  271.             try:
  272.                 func = eval(arg, self.curframe.f_globals, self.curframe.f_locals)
  273.             except:
  274.                 func = arg
  275.  
  276.             
  277.             try:
  278.                 if hasattr(func, 'im_func'):
  279.                     func = func.im_func
  280.                 
  281.                 code = func.func_code
  282.                 funcname = code.co_name
  283.                 lineno = code.co_firstlineno
  284.                 filename = code.co_filename
  285.             (ok, filename, ln) = self.lineinfo(arg)
  286.             if not ok:
  287.                 print '*** The specified object', repr(arg), 'is not a function'
  288.                 print 'or was not found along sys.path.'
  289.                 return None
  290.             
  291.  
  292.             funcname = ok
  293.             lineno = int(ln)
  294.         
  295.  
  296.         if not filename:
  297.             filename = self.defaultFile()
  298.         
  299.         line = self.checkline(filename, lineno)
  300.         if line:
  301.             err = self.set_break(filename, line, temporary, cond, funcname)
  302.             if err:
  303.                 print '***', err
  304.             else:
  305.                 bp = self.get_breaks(filename, line)[-1]
  306.                 print 'Breakpoint %d at %s:%d' % (bp.number, bp.file, bp.line)
  307.         
  308.  
  309.     
  310.     def defaultFile(self):
  311.         '''Produce a reasonable default.'''
  312.         filename = self.curframe.f_code.co_filename
  313.         if filename == '<string>' and self.mainpyfile:
  314.             filename = self.mainpyfile
  315.         
  316.         return filename
  317.  
  318.     do_b = do_break
  319.     
  320.     def do_tbreak(self, arg):
  321.         self.do_break(arg, 1)
  322.  
  323.     
  324.     def lineinfo(self, identifier):
  325.         failed = (None, None, None)
  326.         idstring = identifier.split("'")
  327.         if len(idstring) == 1:
  328.             id = idstring[0].strip()
  329.         elif len(idstring) == 3:
  330.             id = idstring[1].strip()
  331.         else:
  332.             return failed
  333.         if id == '':
  334.             return failed
  335.         
  336.         parts = id.split('.')
  337.         if parts[0] == 'self':
  338.             del parts[0]
  339.             if len(parts) == 0:
  340.                 return failed
  341.             
  342.         
  343.         fname = self.defaultFile()
  344.         if len(parts) == 1:
  345.             item = parts[0]
  346.         else:
  347.             f = self.lookupmodule(parts[0])
  348.             if f:
  349.                 fname = f
  350.             
  351.             item = parts[1]
  352.         answer = find_function(item, fname)
  353.         if not answer:
  354.             pass
  355.         return failed
  356.  
  357.     
  358.     def checkline(self, filename, lineno):
  359.         '''Check whether specified line seems to be executable.
  360.  
  361.         Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
  362.         line or EOF). Warning: testing is not comprehensive.
  363.         '''
  364.         line = linecache.getline(filename, lineno)
  365.         if not line:
  366.             print 'End of file'
  367.             return 0
  368.         
  369.         line = line.strip()
  370.         if not line and line[0] == '#' and line[:3] == '"""' or line[:3] == "'''":
  371.             print '*** Blank or comment'
  372.             return 0
  373.         
  374.         return lineno
  375.  
  376.     
  377.     def do_enable(self, arg):
  378.         args = arg.split()
  379.         for i in args:
  380.             
  381.             try:
  382.                 i = int(i)
  383.             except ValueError:
  384.                 print 'Breakpoint index %r is not a number' % i
  385.                 continue
  386.  
  387.             if i <= i:
  388.                 pass
  389.             elif not i < len(bdb.Breakpoint.bpbynumber):
  390.                 print 'No breakpoint numbered', i
  391.                 continue
  392.             
  393.             bp = bdb.Breakpoint.bpbynumber[i]
  394.             if bp:
  395.                 bp.enable()
  396.                 continue
  397.             0
  398.         
  399.  
  400.     
  401.     def do_disable(self, arg):
  402.         args = arg.split()
  403.         for i in args:
  404.             
  405.             try:
  406.                 i = int(i)
  407.             except ValueError:
  408.                 print 'Breakpoint index %r is not a number' % i
  409.                 continue
  410.  
  411.             if i <= i:
  412.                 pass
  413.             elif not i < len(bdb.Breakpoint.bpbynumber):
  414.                 print 'No breakpoint numbered', i
  415.                 continue
  416.             
  417.             bp = bdb.Breakpoint.bpbynumber[i]
  418.             if bp:
  419.                 bp.disable()
  420.                 continue
  421.             0
  422.         
  423.  
  424.     
  425.     def do_condition(self, arg):
  426.         args = arg.split(' ', 1)
  427.         bpnum = int(args[0].strip())
  428.         
  429.         try:
  430.             cond = args[1]
  431.         except:
  432.             cond = None
  433.  
  434.         bp = bdb.Breakpoint.bpbynumber[bpnum]
  435.         if bp:
  436.             bp.cond = cond
  437.             if not cond:
  438.                 print 'Breakpoint', bpnum, 'is now unconditional.'
  439.             
  440.         
  441.  
  442.     
  443.     def do_ignore(self, arg):
  444.         '''arg is bp number followed by ignore count.'''
  445.         args = arg.split()
  446.         bpnum = int(args[0].strip())
  447.         
  448.         try:
  449.             count = int(args[1].strip())
  450.         except:
  451.             count = 0
  452.  
  453.         bp = bdb.Breakpoint.bpbynumber[bpnum]
  454.         if bp:
  455.             bp.ignore = count
  456.             if count > 0:
  457.                 reply = 'Will ignore next '
  458.                 if count > 1:
  459.                     reply = reply + '%d crossings' % count
  460.                 else:
  461.                     reply = reply + '1 crossing'
  462.                 print reply + ' of breakpoint %d.' % bpnum
  463.             else:
  464.                 print 'Will stop next time breakpoint', bpnum, 'is reached.'
  465.         
  466.  
  467.     
  468.     def do_clear(self, arg):
  469.         '''Three possibilities, tried in this order:
  470.         clear -> clear all breaks, ask for confirmation
  471.         clear file:lineno -> clear all breaks at file:lineno
  472.         clear bpno bpno ... -> clear breakpoints by number'''
  473.         if not arg:
  474.             
  475.             try:
  476.                 reply = raw_input('Clear all breaks? ')
  477.             except EOFError:
  478.                 reply = 'no'
  479.  
  480.             reply = reply.strip().lower()
  481.             if reply in ('y', 'yes'):
  482.                 self.clear_all_breaks()
  483.             
  484.             return None
  485.         
  486.         if ':' in arg:
  487.             i = arg.rfind(':')
  488.             filename = arg[:i]
  489.             arg = arg[i + 1:]
  490.             
  491.             try:
  492.                 lineno = int(arg)
  493.             except ValueError:
  494.                 err = 'Invalid line number (%s)' % arg
  495.  
  496.             err = self.clear_break(filename, lineno)
  497.             if err:
  498.                 print '***', err
  499.             
  500.             return None
  501.         
  502.         numberlist = arg.split()
  503.         for i in numberlist:
  504.             
  505.             try:
  506.                 i = int(i)
  507.             except ValueError:
  508.                 print 'Breakpoint index %r is not a number' % i
  509.                 continue
  510.  
  511.             if i <= i:
  512.                 pass
  513.             elif not i < len(bdb.Breakpoint.bpbynumber):
  514.                 print 'No breakpoint numbered', i
  515.                 continue
  516.             
  517.             err = self.clear_bpbynumber(i)
  518.             if err:
  519.                 print '***', err
  520.                 continue
  521.             print 'Deleted breakpoint', i
  522.         
  523.  
  524.     do_cl = do_clear
  525.     
  526.     def do_where(self, arg):
  527.         self.print_stack_trace()
  528.  
  529.     do_w = do_where
  530.     do_bt = do_where
  531.     
  532.     def do_up(self, arg):
  533.         if self.curindex == 0:
  534.             print '*** Oldest frame'
  535.         else:
  536.             self.curindex = self.curindex - 1
  537.             self.curframe = self.stack[self.curindex][0]
  538.             self.print_stack_entry(self.stack[self.curindex])
  539.             self.lineno = None
  540.  
  541.     do_u = do_up
  542.     
  543.     def do_down(self, arg):
  544.         if self.curindex + 1 == len(self.stack):
  545.             print '*** Newest frame'
  546.         else:
  547.             self.curindex = self.curindex + 1
  548.             self.curframe = self.stack[self.curindex][0]
  549.             self.print_stack_entry(self.stack[self.curindex])
  550.             self.lineno = None
  551.  
  552.     do_d = do_down
  553.     
  554.     def do_step(self, arg):
  555.         self.set_step()
  556.         return 1
  557.  
  558.     do_s = do_step
  559.     
  560.     def do_next(self, arg):
  561.         self.set_next(self.curframe)
  562.         return 1
  563.  
  564.     do_n = do_next
  565.     
  566.     def do_return(self, arg):
  567.         self.set_return(self.curframe)
  568.         return 1
  569.  
  570.     do_r = do_return
  571.     
  572.     def do_continue(self, arg):
  573.         self.set_continue()
  574.         return 1
  575.  
  576.     do_c = do_cont = do_continue
  577.     
  578.     def do_jump(self, arg):
  579.         if self.curindex + 1 != len(self.stack):
  580.             print '*** You can only jump within the bottom frame'
  581.             return None
  582.         
  583.         
  584.         try:
  585.             arg = int(arg)
  586.         except ValueError:
  587.             print "*** The 'jump' command requires a line number."
  588.  
  589.         
  590.         try:
  591.             self.curframe.f_lineno = arg
  592.             self.stack[self.curindex] = (self.stack[self.curindex][0], arg)
  593.             self.print_stack_entry(self.stack[self.curindex])
  594.         except ValueError:
  595.             e = None
  596.             print '*** Jump failed:', e
  597.  
  598.  
  599.     do_j = do_jump
  600.     
  601.     def do_debug(self, arg):
  602.         sys.settrace(None)
  603.         globals = self.curframe.f_globals
  604.         locals = self.curframe.f_locals
  605.         p = Pdb()
  606.         p.prompt = '(%s) ' % self.prompt.strip()
  607.         print 'ENTERING RECURSIVE DEBUGGER'
  608.         sys.call_tracing(p.run, (arg, globals, locals))
  609.         print 'LEAVING RECURSIVE DEBUGGER'
  610.         sys.settrace(self.trace_dispatch)
  611.         self.lastcmd = p.lastcmd
  612.  
  613.     
  614.     def do_quit(self, arg):
  615.         self._user_requested_quit = 1
  616.         self.set_quit()
  617.         return 1
  618.  
  619.     do_q = do_quit
  620.     do_exit = do_quit
  621.     
  622.     def do_EOF(self, arg):
  623.         print 
  624.         self._user_requested_quit = 1
  625.         self.set_quit()
  626.         return 1
  627.  
  628.     
  629.     def do_args(self, arg):
  630.         f = self.curframe
  631.         co = f.f_code
  632.         dict = f.f_locals
  633.         n = co.co_argcount
  634.         if co.co_flags & 4:
  635.             n = n + 1
  636.         
  637.         if co.co_flags & 8:
  638.             n = n + 1
  639.         
  640.         for i in range(n):
  641.             name = co.co_varnames[i]
  642.             print name, '=',
  643.             if name in dict:
  644.                 print dict[name]
  645.                 continue
  646.             print '*** undefined ***'
  647.         
  648.  
  649.     do_a = do_args
  650.     
  651.     def do_retval(self, arg):
  652.         if '__return__' in self.curframe.f_locals:
  653.             print self.curframe.f_locals['__return__']
  654.         else:
  655.             print '*** Not yet returned!'
  656.  
  657.     do_rv = do_retval
  658.     
  659.     def _getval(self, arg):
  660.         
  661.         try:
  662.             return eval(arg, self.curframe.f_globals, self.curframe.f_locals)
  663.         except:
  664.             (t, v) = sys.exc_info()[:2]
  665.             if isinstance(t, str):
  666.                 exc_type_name = t
  667.             else:
  668.                 exc_type_name = t.__name__
  669.             print '***', exc_type_name + ':', repr(v)
  670.             raise 
  671.  
  672.  
  673.     
  674.     def do_p(self, arg):
  675.         
  676.         try:
  677.             print repr(self._getval(arg))
  678.         except:
  679.             pass
  680.  
  681.  
  682.     
  683.     def do_pp(self, arg):
  684.         
  685.         try:
  686.             pprint.pprint(self._getval(arg))
  687.         except:
  688.             pass
  689.  
  690.  
  691.     
  692.     def do_list(self, arg):
  693.         self.lastcmd = 'list'
  694.         last = None
  695.         if arg:
  696.             
  697.             try:
  698.                 x = eval(arg, { }, { })
  699.                 if type(x) == type(()):
  700.                     (first, last) = x
  701.                     first = int(first)
  702.                     last = int(last)
  703.                     if last < first:
  704.                         last = first + last
  705.                     
  706.                 else:
  707.                     first = max(1, int(x) - 5)
  708.             print '*** Error in argument:', repr(arg)
  709.             return None
  710.  
  711.         elif self.lineno is None:
  712.             first = max(1, self.curframe.f_lineno - 5)
  713.         else:
  714.             first = self.lineno + 1
  715.         if last is None:
  716.             last = first + 10
  717.         
  718.         filename = self.curframe.f_code.co_filename
  719.         breaklist = self.get_file_breaks(filename)
  720.         
  721.         try:
  722.             for lineno in range(first, last + 1):
  723.                 line = linecache.getline(filename, lineno)
  724.                 if not line:
  725.                     print '[EOF]'
  726.                     break
  727.                     continue
  728.                 s = repr(lineno).rjust(3)
  729.                 if len(s) < 4:
  730.                     s = s + ' '
  731.                 
  732.                 if lineno in breaklist:
  733.                     s = s + 'B'
  734.                 else:
  735.                     s = s + ' '
  736.                 if lineno == self.curframe.f_lineno:
  737.                     s = s + '->'
  738.                 
  739.                 print s + '\t' + line,
  740.                 self.lineno = lineno
  741.         except KeyboardInterrupt:
  742.             pass
  743.  
  744.  
  745.     do_l = do_list
  746.     
  747.     def do_whatis(self, arg):
  748.         
  749.         try:
  750.             value = eval(arg, self.curframe.f_globals, self.curframe.f_locals)
  751.         except:
  752.             (t, v) = sys.exc_info()[:2]
  753.             if type(t) == type(''):
  754.                 exc_type_name = t
  755.             else:
  756.                 exc_type_name = t.__name__
  757.             print '***', exc_type_name + ':', repr(v)
  758.             return None
  759.  
  760.         code = None
  761.         
  762.         try:
  763.             code = value.func_code
  764.         except:
  765.             pass
  766.  
  767.         if code:
  768.             print 'Function', code.co_name
  769.             return None
  770.         
  771.         
  772.         try:
  773.             code = value.im_func.func_code
  774.         except:
  775.             pass
  776.  
  777.         if code:
  778.             print 'Method', code.co_name
  779.             return None
  780.         
  781.         print type(value)
  782.  
  783.     
  784.     def do_alias(self, arg):
  785.         args = arg.split()
  786.         if len(args) == 0:
  787.             keys = self.aliases.keys()
  788.             keys.sort()
  789.             for alias in keys:
  790.                 print '%s = %s' % (alias, self.aliases[alias])
  791.             
  792.             return None
  793.         
  794.         if args[0] in self.aliases and len(args) == 1:
  795.             print '%s = %s' % (args[0], self.aliases[args[0]])
  796.         else:
  797.             self.aliases[args[0]] = ' '.join(args[1:])
  798.  
  799.     
  800.     def do_unalias(self, arg):
  801.         args = arg.split()
  802.         if len(args) == 0:
  803.             return None
  804.         
  805.         if args[0] in self.aliases:
  806.             del self.aliases[args[0]]
  807.         
  808.  
  809.     
  810.     def print_stack_trace(self):
  811.         
  812.         try:
  813.             for frame_lineno in self.stack:
  814.                 self.print_stack_entry(frame_lineno)
  815.         except KeyboardInterrupt:
  816.             pass
  817.  
  818.  
  819.     
  820.     def print_stack_entry(self, frame_lineno, prompt_prefix = line_prefix):
  821.         (frame, lineno) = frame_lineno
  822.         if frame is self.curframe:
  823.             print '>',
  824.         else:
  825.             print ' ',
  826.         print self.format_stack_entry(frame_lineno, prompt_prefix)
  827.  
  828.     
  829.     def help_help(self):
  830.         self.help_h()
  831.  
  832.     
  833.     def help_h(self):
  834.         print 'h(elp)\nWithout argument, print the list of available commands.\nWith a command name as argument, print help about that command\n"help pdb" pipes the full documentation file to the $PAGER\n"help exec" gives help on the ! command'
  835.  
  836.     
  837.     def help_where(self):
  838.         self.help_w()
  839.  
  840.     
  841.     def help_w(self):
  842.         print 'w(here)\nPrint a stack trace, with the most recent frame at the bottom.\nAn arrow indicates the "current frame", which determines the\ncontext of most commands.  \'bt\' is an alias for this command.'
  843.  
  844.     help_bt = help_w
  845.     
  846.     def help_down(self):
  847.         self.help_d()
  848.  
  849.     
  850.     def help_d(self):
  851.         print 'd(own)\nMove the current frame one level down in the stack trace\n(to a newer frame).'
  852.  
  853.     
  854.     def help_up(self):
  855.         self.help_u()
  856.  
  857.     
  858.     def help_u(self):
  859.         print 'u(p)\nMove the current frame one level up in the stack trace\n(to an older frame).'
  860.  
  861.     
  862.     def help_break(self):
  863.         self.help_b()
  864.  
  865.     
  866.     def help_b(self):
  867.         print "b(reak) ([file:]lineno | function) [, condition]\nWith a line number argument, set a break there in the current\nfile.  With a function name, set a break at first executable line\nof that function.  Without argument, list all breaks.  If a second\nargument is present, it is a string specifying an expression\nwhich must evaluate to true before the breakpoint is honored.\n\nThe line number may be prefixed with a filename and a colon,\nto specify a breakpoint in another file (probably one that\nhasn't been loaded yet).  The file is searched for on sys.path;\nthe .py suffix may be omitted."
  868.  
  869.     
  870.     def help_clear(self):
  871.         self.help_cl()
  872.  
  873.     
  874.     def help_cl(self):
  875.         print 'cl(ear) filename:lineno'
  876.         print 'cl(ear) [bpnumber [bpnumber...]]\nWith a space separated list of breakpoint numbers, clear\nthose breakpoints.  Without argument, clear all breaks (but\nfirst ask confirmation).  With a filename:lineno argument,\nclear all breaks at that line in that file.\n\nNote that the argument is different from previous versions of\nthe debugger (in python distributions 1.5.1 and before) where\na linenumber was used instead of either filename:lineno or\nbreakpoint numbers.'
  877.  
  878.     
  879.     def help_tbreak(self):
  880.         print 'tbreak  same arguments as break, but breakpoint is\nremoved when first hit.'
  881.  
  882.     
  883.     def help_enable(self):
  884.         print 'enable bpnumber [bpnumber ...]\nEnables the breakpoints given as a space separated list of\nbp numbers.'
  885.  
  886.     
  887.     def help_disable(self):
  888.         print 'disable bpnumber [bpnumber ...]\nDisables the breakpoints given as a space separated list of\nbp numbers.'
  889.  
  890.     
  891.     def help_ignore(self):
  892.         print 'ignore bpnumber count\nSets the ignore count for the given breakpoint number.  A breakpoint\nbecomes active when the ignore count is zero.  When non-zero, the\ncount is decremented each time the breakpoint is reached and the\nbreakpoint is not disabled and any associated condition evaluates\nto true.'
  893.  
  894.     
  895.     def help_condition(self):
  896.         print 'condition bpnumber str_condition\nstr_condition is a string specifying an expression which\nmust evaluate to true before the breakpoint is honored.\nIf str_condition is absent, any existing condition is removed;\ni.e., the breakpoint is made unconditional.'
  897.  
  898.     
  899.     def help_step(self):
  900.         self.help_s()
  901.  
  902.     
  903.     def help_s(self):
  904.         print 's(tep)\nExecute the current line, stop at the first possible occasion\n(either in a function that is called or in the current function).'
  905.  
  906.     
  907.     def help_next(self):
  908.         self.help_n()
  909.  
  910.     
  911.     def help_n(self):
  912.         print 'n(ext)\nContinue execution until the next line in the current function\nis reached or it returns.'
  913.  
  914.     
  915.     def help_return(self):
  916.         self.help_r()
  917.  
  918.     
  919.     def help_r(self):
  920.         print 'r(eturn)\nContinue execution until the current function returns.'
  921.  
  922.     
  923.     def help_continue(self):
  924.         self.help_c()
  925.  
  926.     
  927.     def help_cont(self):
  928.         self.help_c()
  929.  
  930.     
  931.     def help_c(self):
  932.         print 'c(ont(inue))\nContinue execution, only stop when a breakpoint is encountered.'
  933.  
  934.     
  935.     def help_jump(self):
  936.         self.help_j()
  937.  
  938.     
  939.     def help_j(self):
  940.         print 'j(ump) lineno\nSet the next line that will be executed.'
  941.  
  942.     
  943.     def help_debug(self):
  944.         print 'debug code\nEnter a recursive debugger that steps through the code argument\n(which is an arbitrary expression or statement to be executed\nin the current environment).'
  945.  
  946.     
  947.     def help_list(self):
  948.         self.help_l()
  949.  
  950.     
  951.     def help_l(self):
  952.         print 'l(ist) [first [,last]]\nList source code for the current file.\nWithout arguments, list 11 lines around the current line\nor continue the previous listing.\nWith one argument, list 11 lines starting at that line.\nWith two arguments, list the given range;\nif the second argument is less than the first, it is a count.'
  953.  
  954.     
  955.     def help_args(self):
  956.         self.help_a()
  957.  
  958.     
  959.     def help_a(self):
  960.         print 'a(rgs)\nPrint the arguments of the current function.'
  961.  
  962.     
  963.     def help_p(self):
  964.         print 'p expression\nPrint the value of the expression.'
  965.  
  966.     
  967.     def help_pp(self):
  968.         print 'pp expression\nPretty-print the value of the expression.'
  969.  
  970.     
  971.     def help_exec(self):
  972.         print "(!) statement\nExecute the (one-line) statement in the context of\nthe current stack frame.\nThe exclamation point can be omitted unless the first word\nof the statement resembles a debugger command.\nTo assign to a global variable you must always prefix the\ncommand with a 'global' command, e.g.:\n(Pdb) global list_options; list_options = ['-l']\n(Pdb)"
  973.  
  974.     
  975.     def help_quit(self):
  976.         self.help_q()
  977.  
  978.     
  979.     def help_q(self):
  980.         print 'q(uit) or exit - Quit from the debugger.\nThe program being executed is aborted.'
  981.  
  982.     help_exit = help_q
  983.     
  984.     def help_whatis(self):
  985.         print 'whatis arg\nPrints the type of the argument.'
  986.  
  987.     
  988.     def help_EOF(self):
  989.         print 'EOF\nHandles the receipt of EOF as a command.'
  990.  
  991.     
  992.     def help_alias(self):
  993.         print 'alias [name [command [parameter parameter ...] ]]\nCreates an alias called \'name\' the executes \'command\'.  The command\nmust *not* be enclosed in quotes.  Replaceable parameters are\nindicated by %1, %2, and so on, while %* is replaced by all the\nparameters.  If no command is given, the current alias for name\nis shown. If no name is given, all aliases are listed.\n\nAliases may be nested and can contain anything that can be\nlegally typed at the pdb prompt.  Note!  You *can* override\ninternal pdb commands with aliases!  Those internal commands\nare then hidden until the alias is removed.  Aliasing is recursively\napplied to the first word of the command line; all other words\nin the line are left alone.\n\nSome useful aliases (especially when placed in the .pdbrc file) are:\n\n#Print instance variables (usage "pi classInst")\nalias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]\n\n#Print instance variables in self\nalias ps pi self\n'
  994.  
  995.     
  996.     def help_unalias(self):
  997.         print 'unalias name\nDeletes the specified alias.'
  998.  
  999.     
  1000.     def help_pdb(self):
  1001.         help()
  1002.  
  1003.     
  1004.     def lookupmodule(self, filename):
  1005.         '''Helper function for break/clear parsing -- may be overridden.
  1006.  
  1007.         lookupmodule() translates (possibly incomplete) file or module name
  1008.         into an absolute file name.
  1009.         '''
  1010.         if os.path.isabs(filename) and os.path.exists(filename):
  1011.             return filename
  1012.         
  1013.         f = os.path.join(sys.path[0], filename)
  1014.         if os.path.exists(f) and self.canonic(f) == self.mainpyfile:
  1015.             return f
  1016.         
  1017.         (root, ext) = os.path.splitext(filename)
  1018.         if ext == '':
  1019.             filename = filename + '.py'
  1020.         
  1021.         if os.path.isabs(filename):
  1022.             return filename
  1023.         
  1024.         for dirname in sys.path:
  1025.             while os.path.islink(dirname):
  1026.                 dirname = os.readlink(dirname)
  1027.             fullname = os.path.join(dirname, filename)
  1028.             if os.path.exists(fullname):
  1029.                 return fullname
  1030.                 continue
  1031.         
  1032.  
  1033.     
  1034.     def _runscript(self, filename):
  1035.         globals_ = {
  1036.             '__name__': '__main__' }
  1037.         locals_ = globals_
  1038.         self._wait_for_mainpyfile = 1
  1039.         self.mainpyfile = self.canonic(filename)
  1040.         self._user_requested_quit = 0
  1041.         statement = 'execfile( "%s")' % filename
  1042.         self.run(statement, globals = globals_, locals = locals_)
  1043.  
  1044.  
  1045.  
  1046. def run(statement, globals = None, locals = None):
  1047.     Pdb().run(statement, globals, locals)
  1048.  
  1049.  
  1050. def runeval(expression, globals = None, locals = None):
  1051.     return Pdb().runeval(expression, globals, locals)
  1052.  
  1053.  
  1054. def runctx(statement, globals, locals):
  1055.     run(statement, globals, locals)
  1056.  
  1057.  
  1058. def runcall(*args, **kwds):
  1059.     return Pdb().runcall(*args, **kwds)
  1060.  
  1061.  
  1062. def set_trace():
  1063.     Pdb().set_trace(sys._getframe().f_back)
  1064.  
  1065.  
  1066. def post_mortem(t):
  1067.     p = Pdb()
  1068.     p.reset()
  1069.     while t.tb_next is not None:
  1070.         t = t.tb_next
  1071.     p.interaction(t.tb_frame, t)
  1072.  
  1073.  
  1074. def pm():
  1075.     post_mortem(sys.last_traceback)
  1076.  
  1077. TESTCMD = 'import x; x.main()'
  1078.  
  1079. def test():
  1080.     run(TESTCMD)
  1081.  
  1082.  
  1083. def help():
  1084.     for dirname in sys.path:
  1085.         fullname = os.path.join(dirname, 'pdb.doc')
  1086.         if os.path.exists(fullname):
  1087.             sts = os.system('${PAGER-more} ' + fullname)
  1088.             if sts:
  1089.                 print '*** Pager exit status:', sts
  1090.             
  1091.             break
  1092.             continue
  1093.     else:
  1094.         print 'Sorry, can\'t find the help file "pdb.doc"', 'along the Python search path'
  1095.  
  1096.  
  1097. def main():
  1098.     if not sys.argv[1:]:
  1099.         print 'usage: pdb.py scriptfile [arg] ...'
  1100.         sys.exit(2)
  1101.     
  1102.     mainpyfile = sys.argv[1]
  1103.     if not os.path.exists(mainpyfile):
  1104.         print 'Error:', mainpyfile, 'does not exist'
  1105.         sys.exit(1)
  1106.     
  1107.     del sys.argv[0]
  1108.     sys.path[0] = os.path.dirname(mainpyfile)
  1109.     pdb = Pdb()
  1110.     while None:
  1111.         
  1112.         try:
  1113.             pdb._runscript(mainpyfile)
  1114.             if pdb._user_requested_quit:
  1115.                 break
  1116.             
  1117.             print 'The program finished and will be restarted'
  1118.         continue
  1119.         except SystemExit:
  1120.             print 'The program exited via sys.exit(). Exit status: ', sys.exc_info()[1]
  1121.             continue
  1122.             traceback.print_exc()
  1123.             print 'Uncaught exception. Entering post mortem debugging'
  1124.             print "Running 'cont' or 'step' will restart the program"
  1125.             t = sys.exc_info()[2]
  1126.             while t.tb_next is not None:
  1127.                 t = t.tb_next
  1128.             pdb.interaction(t.tb_frame, t)
  1129.             print 'Post mortem debugger finished. The ' + mainpyfile + ' will be restarted'
  1130.             continue
  1131.         
  1132.  
  1133.  
  1134. if __name__ == '__main__':
  1135.     main()
  1136.  
  1137.